package com.etc.team2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.team2.bean.Station;
import com.etc.team2.service.StationService;

public class AllStationServlet extends HttpServlet {
	
	/**
	 * 用于获取所有站台信息的Servlet
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 调用StationService中的getAllStation()方法，获取全部站台信息，发送给stationupdate.jsp方法
		 */
		String referer=request.getHeader("referer");
		if (referer==null||!referer.startsWith("http://localhost:")) {
			response.sendRedirect("home.jsp");
			return;
		}
		StationService ss=new StationService();
		List<Station> stationslist=ss.getAllStation();
		int pageSum=0;
		if(stationslist.size()%5==0)
		{
			pageSum=stationslist.size()/5;
		}
		else
		{
			pageSum=stationslist.size()/5+1;
		}
		int pageNum=1;
		if(request.getParameter("pageNum")!=null)
		{
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}
		if(pageNum<1)
		{
			pageNum=pageSum;
		}
		else if(pageNum>pageSum)
		{
			pageNum=1;
		}
		List<Station> stationlist=new ArrayList<Station>();
		int n=5;
		if(pageNum==pageSum)
		{
			n=stationslist.size()%5;
		}
		for(int i=0;i<n;i++)
		{
			stationlist.add(stationslist.get((pageNum-1)*5+i));
		}
		
		
		request.setAttribute("stationslist", stationlist);
		request.setAttribute("pageNum", pageNum);
		request.getRequestDispatcher("stationshow.jsp").forward(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
